package com.jc.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtil {

	
	
	
	 /**
     * @param s
     * @return 获得图片
     */
    public static List<String> getImg(String s)
    {
        String regex;
        List<String> list = new ArrayList<String>();
        regex = "src=\"(.*?)\"";
        Pattern pa = Pattern.compile(regex, Pattern.UNICODE_CASE);
        Matcher ma = pa.matcher(s);
        while (ma.find())
        {
            list.add(ma.group());
        }
        return list;
    }
    
    /***
     * 
    * @Title: getImageSrc
    * @Description: TODO
    * @param @param htmlCode
    * @param @return
    * @return List<String>
    * @throws
     */
    public static List<String> getImageSrc(String htmlCode) {
        List<String> imageSrcList = new ArrayList<String>();
        Pattern p = Pattern.compile("<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(htmlCode);
        String quote = null;
        String src = null;
        while (m.find()) {
            quote = m.group(1);
            src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("\\s+")[0] : m.group(2);
            imageSrcList.add(src);
        }
        return imageSrcList;
    }
    
    public static void main(String[] args) {
 	String str = "\"<p><img src=\"http://yanxuan.nosdn.127.net/e3fd4d5e92778cc4cef80c8770d0108c.jpg\" _src=\"http://yanxuan.nosdn.127.net/e3fd4d5e92778cc4cef80c8770d0108c.jpg\" style=\"\"/></p><p><img src=\"http://yanxuan.nosdn.127.net/40b388b5741f0242d561ae8ded190df4.jpg\" _src=\"http://yanxuan.nosdn.127.net/40b388b5741f0242d561ae8ded190df4.jpg\" style=\"\"/></p><p><img src=\"http://yanxuan.nosdn.127.net/659f31c0810905ddd7ec81a7753693bf.jpg\" _src=\"http://yanxuan.nosdn.127.net/659f31c0810905ddd7ec81a7753693bf.jpg\" style=\"\"/></p><p><img src=\"http://yanxuan.nosdn.127.net/a0bdc573b73b0bc1cdde3dc1191d264f.jpg\" _src=\"http://yanxuan.nosdn.127.net/a0bdc573b73b0bc1cdde3dc1191d264f.jpg\" style=\"\"/></p><p><img src=\"http://yanxuan.nosdn.127.net/5556c85a48a4b25a4b8cd6b9ca0724b2.jpg\" _src=\"http://yanxuan.nosdn.127.net/5556c85a48a4b25a4b8cd6b9ca0724b2.jpg\" style=\"\"/></p><p><br/></p>\"";
    	
    	
    	
//    	String urls[] = getImgaddress(str);
//    	for(String ul : urls) {
//    		System.out.println("======" + ul);
//    	}
//    	
//    	
//    	List<String> ls = getImg(str);
//    	for(String st : ls) {
//    		System.out.println("-----" + st);
//    	}
    	
    	
    	List<String> mystr = getImageSrc(str);
    	for(String im : mystr) {
    		System.out.println("*********  " + im);
    	}
	}
    
    
}
